import java.awt.AWTException;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class PictureAnaylser {

	public static Rectangle screenRectangleGOLD;
	public static Rectangle screenRectangleXP;
	public static BufferedImage[] matrixImages;
	final static String MAX_XP = "46080000";

	PictureAnaylser() {
	}

	public static String analyze(BufferedImage image, int resolution) {
		String result = "";
		boolean whiteFound = false;
		for (int y = 0; y < image.getHeight(); y++) {
			for (int x = 0; x < image.getWidth(); x++) {
				if (image.getRGB(x, y) == Color.white.getRGB()) {
					for (int i = 0; i < 10; i++) {
						boolean foundONE = true;
						for (int length = 0; length < matrixImages[i]
								.getWidth(); length++) {
							for (int height = 0; height < matrixImages[i]
									.getHeight(); height++) {
								if (x + length < image.getWidth()
										&& y + height < image.getHeight()) {
									if (image.getRGB(x + length, y + height) == matrixImages[i]
											.getRGB(length, height)) {
										foundONE &= true;
									} else {
										foundONE &= false;
									}
								} else {
									break;
								}

							}
						}
						if (foundONE) {
							result += i;
							x += matrixImages[i].getWidth();
						}
					}
				}

			}
			if (whiteFound) {
				break;
			}

		}

		return result;
	}

	public static void analyizeFiles() throws IOException {
		for (int k = 1; k < 7; k++) {
			BufferedImage image = ImageIO
					.read(new File("C:/test/" + k + ".png"));

			BufferedImage outputXP = new BufferedImage(159, 18,
					BufferedImage.TYPE_INT_ARGB);
			// fill XP image
			for (int i = 731; i < 890; i++) {
				for (int j = 762; j < 780; j++) {
					if (image.getRGB(i, j) >= new Color(68, 68, 68).getRGB()) {
						image.setRGB(i, j, Color.WHITE.getRGB());
					} else {
						image.setRGB(i, j, Color.BLACK.getRGB());
					}
					outputXP.setRGB(i - 731, j - 762, image.getRGB(i, j));
				}
			}

			BufferedImage outputGold = new BufferedImage(159, 18,
					BufferedImage.TYPE_INT_ARGB);
			// fill gold image
			for (int i = 1063; i < 1163; i++) {
				for (int j = 433; j < 451; j++) {
					if (image.getRGB(i, j) >= new Color(68, 68, 68).getRGB()) {
						image.setRGB(i, j, Color.WHITE.getRGB());
					} else {
						image.setRGB(i, j, Color.BLACK.getRGB());
					}
					outputGold.setRGB(i - 1063, j - 433, image.getRGB(i, j));
				}
			}

			String test = analyze(outputXP, 1).replaceAll(MAX_XP, "");
			// System.out.println(test);
			// output XP
			File outputFileXP = new File("C:/test/" + k + "_output_XP.png");
			ImageIO.write(outputXP, "png", outputFileXP);
			// output GOLD
			File outputFileGOLD = new File("C:/test/" + k + "_output_GOLD.png");
			ImageIO.write(outputGold, "png", outputFileGOLD);
		}
	}

	public BufferedImage[] optimizeImage(BufferedImage[] images) {
		BufferedImage[] result_array = new BufferedImage[images.length];
		for (int x = 0; x < images.length; x++) {
			BufferedImage result = new BufferedImage(images[x].getWidth(),
					images[x].getHeight(), BufferedImage.TYPE_INT_ARGB);
			for (int i = 0; i < images[x].getWidth(); i++) {
				for (int j = 0; j < images[x].getHeight(); j++) {
					if (images[x].getRGB(i, j) >= new Color(68, 68, 68)
							.getRGB()) {
						images[x].setRGB(i, j, Color.WHITE.getRGB());
					} else {
						images[x].setRGB(i, j, Color.BLACK.getRGB());
					}
					result.setRGB(i, j, images[x].getRGB(i, j));
				}
			}
			result_array[x] = result;
		}
		return result_array;
	}

	public static BufferedImage[] captureGOLD_XP() throws AWTException,
			IOException {
		// System.out.println("capture Screen");
		// System.out.println("gold screenspace " + screenRectangleGOLD);
		// System.out.println("XP screenspace " + screenRectangleXP);
		// init result
		// result[0] = Gold
		// result[1] = XP
		BufferedImage[] result = new BufferedImage[2];

		Robot robot;

		// init robot
		robot = new Robot();
		result[0] = robot.createScreenCapture(screenRectangleGOLD);

		result[1] = robot.createScreenCapture(screenRectangleXP);

		// return result
		return result;
	}
}
